//
//  ReleaseDynamicViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/12.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "ReleaseDynamicViewModel.h"
#import "ReleaseDynamicReusableView.h"
#import "ReleaseDynamicCollectionViewCell.h"
#import "PopupView.h"
#import "ZZPhoto.h"

@interface ReleaseDynamicViewModel()<UICollectionViewDelegate, UICollectionViewDataSource, YLJCollectionViewFlowLayoutDelegate, UITextViewDelegate>
{
    NSString *textVStr; // textView输入文字
    NSMutableArray * dataArr;
    UICollectionView *selfCollectionView;
}

@end

@implementation ReleaseDynamicViewModel


- (instancetype)initWithViewToViewCollectionView:(UICollectionView *)collectionView theFlowLayout:(YLJCollectionViewFlowLayout *)flowlayout theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewCollectionView:collectionView theFlowLayout:flowlayout theSuperView:superView]) {
        dataArr = [NSMutableArray array];
        self.cout = 0;
        collectionView.y = collectionView.y+ kHeightProportion_In_iPhoneTriploid(10);
        collectionView.height = collectionView.height - kHeightProportion_In_iPhoneTriploid(10);
        flowlayout.delegate = self;
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [collectionView registerClass:[ReleaseDynamicReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReleaseDynamicReusableView"];
        [collectionView registerClass:[ReleaseDynamicCollectionViewCell class] forCellWithReuseIdentifier:@"ReleaseDynamicCollectionViewCell"];
        selfCollectionView = collectionView;
        [superView addSubview:collectionView];
    }
    return self;
}

#pragma mark --- UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataArr.count + 1;
}

- (ZLLayoutType)collectionView:(UICollectionView *)collectionView layout:(YLJCollectionViewFlowLayout *)collectionViewLayout typeOfLayout:(NSInteger)section
{
    return ClosedLayout;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KScreenWidth, kHeightProportion_In_iPhoneTriploid(121));
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ReleaseDynamicReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReleaseDynamicReusableView" forIndexPath:indexPath];
    view.textV.delegate = self;
    return view;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReleaseDynamicCollectionViewCell * cell = [ReleaseDynamicCollectionViewCell cellWithCollectionView:collectionView forIndexPath:indexPath];
    if (dataArr.count > 0 && indexPath.row != dataArr.count) {
        DLog(@"==%ld",(long)indexPath.row);
        ZZPhoto *photo = dataArr[indexPath.row];
        cell.backImg = photo.originImage;
        cell.hideDeleBtn = NO;
        cell.index = indexPath;
        kWeakSelf(self);
        cell.BlockDeletPhoto = ^(NSIndexPath *index) {
            [weakself deletePhoto:index];
        };
    } else {
        cell.backImg = [UIImage imageNamed:@""];
        cell.hideDeleBtn = YES;
    }
    return cell;
}

//如果是百分比布局必须实现该代理，设置每个item的百分比，如果没实现默认比例为1
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(YLJCollectionViewFlowLayout*)collectionViewLayout percentOfRow:(NSIndexPath*)indexPath
{
    return 1;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kWidthProportion_In_iPhoneTriploid(100), kWidthProportion_In_iPhoneTriploid(100));
}

//如果是绝对定位布局必须是否该代理，设置每个item的frame
- (CGRect)collectionView:(UICollectionView *)collectionView layout:(YLJCollectionViewFlowLayout*)collectionViewLayout rectOfItem:(NSIndexPath*)indexPath
{
    return CGRectZero;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(YLJCollectionViewFlowLayout *)collectionViewLayout zIndexOfItem:(NSIndexPath*)indexPath
{
    return 0;
}
- (NSInteger)collectionView:(UICollectionView * __nullable)collectionView layout:(YLJCollectionViewFlowLayout* __nullable)collectionViewLayout columnCountOfSection:(NSInteger)section
{
    return 3;
}
//行间距(横)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(10);
}
//竖
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return kWidthProportion_In_iPhoneTriploid(15);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(kHeightProportion_In_iPhoneTriploid(0), (KScreenWidth - kWidthProportion_In_iPhoneTriploid(330))/2, kHeightProportion_In_iPhoneTriploid(8), (KScreenWidth - kWidthProportion_In_iPhoneTriploid(330))/2);//top, left, bottom, right;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArr.count <= 0 || (dataArr.count > 0 && indexPath.row == dataArr.count)) {
        PopupView *popView = [[PopupView alloc] initPopupview:@[@"拍照",@"从相册中选择"]];
        kWeakSelf(self);
        popView.Block_SelectType = ^(NSInteger row) {
            [weakself seletRow:row];
        };
    }
}

- (void)seletRow:(NSInteger)row
{
    if (self.BlockChoosePhones) {
        self.BlockChoosePhones(row);
    }
}

- (void)setArr:(NSMutableArray *)arr
{
    [dataArr addObjectsFromArray:arr];
    self.cout = dataArr.count;
    [selfCollectionView reloadData];
}

// 删除照片
- (void)deletePhoto:(NSIndexPath *)index
{
    [dataArr removeObjectAtIndex:index.row];
    self.cout = dataArr.count;
    [selfCollectionView reloadData];
}

#pragma mark - textViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    NSString *str = [NSString stringWithTextFiledStr:textView.text replacementString:text];
    if (str.length > 360) {
        textVStr = str;
        return NO;
    }
    textVStr = str;
    return YES;
}

- (void)plus
{
    // 请求完接口返回
    if (self.BlockPluseDone) {
        self.BlockPluseDone();
    }
}

@end
